<template>
  <div>
    <el-card>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>内容管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <el-select
            v-model="form.channel_id"
            placeholder="请选择频道"
            clearable
          >
            <el-option
              :label="obj.name"
              :value="obj.id"
              v-for="obj in channelArr"
              :key="obj.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动时间">
          <el-col>
            <el-date-picker
              v-model="timeArr"
              type="daterange"
              @change="changeFn"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-col>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin-top: 20px">
      <p>根据内容筛选查询到{{ totSum }}条结果</p>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="封面">
          <template slot-scope="scope">
            <img :src="scope.row.cover.images" alt="" height="80" width="80" />
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column label="状态">
          <template v-slot="scope">
            <el-tag v-if="scope.row.status === 0" type="info">草稿</el-tag>
            <el-tag v-if="scope.row.status === 1">待审核</el-tag>
            <el-tag v-if="scope.row.status === 2" type="success"
              >审核通过</el-tag
            >
            <el-tag v-if="scope.row.status === 3" type="warning"
              >审核失败</el-tag
            >
            <el-tag v-if="scope.row.status === 4" type="danger">已删除</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="read_count" label="阅读数"> </el-table-column>
        <el-table-column prop="comment_count" label="评论"> </el-table-column>
        <el-table-column prop="like_count" label="点赞"> </el-table-column>
        <el-table-column prop="pubdate" label="发布时间"> </el-table-column>
        <el-table-column prop="status" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="editFn(scope.row.id)"
              plain
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="delFn(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="sizes, prev, pager, next, jumper, ->, total, slot"
        :total="totSum"
         :page-size="form.per_page"
         @current-change="currentFn"
         @size-change="sizeChangeFn"
        :page-sizes="[2,5,8,10]"
        style="margin-top: 20px"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { getChannelAPI, getArticleAPI, delArticleAPI } from '@/api'
export default {
  data () {
    return {
      form: {
        status: null,
        channel_id: '',
        begin_pubdate: '',
        end_pubdate: '',
        page: 1,
        per_page: 2
      },
      timeArr: '',
      channelArr: [],
      tableData: [],
      totSum: 0
    }
  },

  async created () {
    // 获取频道列表
    const res = await getChannelAPI()
    this.channelArr = res.data.data.channels
    // 获取文章列表
    this.getArticle()
  },
  methods: {
    // 编辑
    editFn (id) {
      this.$router.push({
        path: '/layout/publish',
        query: {
          id
        }
      })
    },
    // 改变当前页函数
    currentFn (current) {
      this.form.page = current
      this.getArticle()
    },
    // 改变每页的文章触发时的函数
    sizeChangeFn (sum) {
      this.form.per_page = sum
      this.getArticle()
    },
    // 封装文章列表
    async getArticle () {
      const article = await getArticleAPI({ ...this.form })
      console.log(article)
      this.tableData = article.data.data.results
      //   console.log(article.data.data.total_count)
      this.totSum = article.data.data.total_count
      if (this.tableData.length === 0 && this.form.page > 1) {
        this.form.page--
        this.getArticle()
      }
    },
    onSubmit () {
      this.getArticle()
      console.log('submit!')
    },
    // 删除功能
    async delFn (id) {
      console.log(id)
      await delArticleAPI(id)
      this.getArticle()
    },
    // 日期改变
    changeFn () {
      //   console.log(value)
      this.form.begin_pubdate = this.timeArr[0]
      this.form.end_pubdate = this.timeArr[1]
    }
  }
}
</script>
<style>
</style>
